#!/usr/bin/env python

import gtk
import gettext
import os
from mxit import handles
from blend import config
from blend import autoAway
gettext.install('blendIT','locale');

class signals:

   def __init__(self,blend):
      self.blend = blend;

   def on_cancel(self,widget):
      self.blend.gui.preferencesWindow.hide();
      self.blend.gui.preferencesWindow.destroy();

   def on_save(self,widget):
      window = self.blend.gui.preferencesWindow;
      entries = window.entries;
      cfg = self.blend.config;
      
      changed = False;
      
      # Notifications
      if cfg.notifications['text'] != entries['textNote'].get_active():
         changed = True;
         cfg.notifications['text'] = entries['textNote'].get_active();

      if cfg.notifications['sounds'][0] != entries['soundNote'].get_active():
         changed = True;
         cfg.notifications['sounds'][0] = entries['soundNote'].get_active();

      for i in range(0,len(entries['sounds'])):
         entry = entries['sounds'][i][0];
         key = entries['sounds'][i][1];
         if cfg.notifications['sounds'][1][key] != entry.get_text():
            changed = True;
            cfg.notifications['sounds'][1][key] = entry.get_text();
      
      # Advanced Settings
      if cfg.host != entries['serverHost'].get_text():
         changed = True;
         cfg.host = entries['serverHost'].get_text();

      try:
         if cfg.port != int(entries['serverPort'].get_text()):
            changed = True;
            cfg.port = int(entries['serverPort'].get_text());
      except:
         if cfg.port != 9119:
            changed = True;
            cfg.port = 9119;
      
      if cfg.timeformat != entries['timeFormat'].get_text():
         changed = True;
         cfg.timeformat = entries['timeFormat'].get_text();

      if cfg.debug != entries['debugMode'].get_active():
         changed = True;
         cfg.debug = entries['debugMode'].get_active();
      
      # General settings
      if entries['alias'].get_text() == "":
         entries['alias'].set_text("Me");

      if self.blend.currentAccount.alias != entries['alias'].get_text():
         self.blend.currentAccount.alias = entries['alias'].get_text();

      if cfg.log != entries['log'].get_active():
         cfg.log = entries['log'].get_active();
      
      if cfg.hideOfflineContacts != entries['hideOffline'].get_active():
         changed = True;
         cfg.hideOfflineContacts = entries['hideOffline'].get_active();
      
      if cfg.offlineMessage != (entries['offlineMsgEnable'].get_active(),entries['offlineMsg'].get_text()):
         changed = True;
         cfg.offlineMessage = (entries['offlineMsgEnable'].get_active(),entries['offlineMsg'].get_text());
      
      if cfg.autoAway != (entries['autoAwayEnable'].get_active(),int(entries['autoAway'].get_value())):
         changed = True;
         cfg.autoAway = (entries['autoAwayEnable'].get_active(),int(entries['autoAway'].get_value()));
         if cfg.autoAway[0]:
            if not self.blend.autoAway:
               self.blend.autoAway = autoAway.autoAway(self.blend);
         else:
            self.blend.autoAway = None;
            
      
      model = entries['language'].get_model();
      iter = entries['language'].get_active_iter();
      if self.blend.currentAccount.lang != model.get_value(iter,1):
         changed = True;
         self.blend.currentAccount.lang = model.get_value(iter,1);

      colour = self.__color_to_hex(entries['msgColour'].get_color());
      if cfg.msgColour != (entries['msgColourEnable'].get_active(),colour.upper()):
         changed = True;
         cfg.msgColour = (entries['msgColourEnable'].get_active(),colour);
      
      # Now we store this in settings.xml
      if changed == True:
         c = config.config();
         c.save(cfg,self.blend.currentAccount);
         self.blend.config = cfg;
      
      window.destroy();

   def msgColourEnable(self,widget):
      if widget.get_active():
         self.blend.gui.preferencesWindow.entries['msgColour'].set_sensitive(True);
      else:
         self.blend.gui.preferencesWindow.entries['msgColour'].set_sensitive(False);


   def offlineMsgEnable(self,widget):
      if widget.get_active():
         self.blend.gui.preferencesWindow.entries['offlineMsg'].set_sensitive(True);
      else:
         self.blend.gui.preferencesWindow.entries['offlineMsg'].set_sensitive(False);
         
   def autoAwayEnable(self,widget):
      if widget.get_active():
         self.blend.gui.preferencesWindow.entries['autoAway'].set_sensitive(True);
      else:
         self.blend.gui.preferencesWindow.entries['autoAway'].set_sensitive(False);      

   # Attribution: https://forja.rediris.es/plugins/scmsvn/viewcvs.php/*checkout*/src/Editor/editor.py?root=csl2-sdocente&rev=32
   def __color_to_hex(self,colour):
      hexstring = ""
      for col in 'red','green','blue':
         hexfrag = hex(getattr(colour,col) / (16 * 16)).split("x")[1]
         if len(hexfrag) < 2:
            hexfrag = "0" + hexfrag
         
         hexstring += hexfrag
      return "#" + hexstring